package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;

import flc.ast.adapter.ProVideoAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.ActivityProVideoBinding;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;
import stark.common.basic.utils.UriUtil;

public class ProVideoActivity extends BaseAc<ActivityProVideoBinding> {

    public static List<MyVideoBean> listPlay = new ArrayList<>();//播放列表
    private ProVideoAdapter proVideoAdapter;
    private String videoPath = "";
    private long videoLength;
    private int selPlayPos = 0;
    private CastScreenManager mCastScreenManager;

    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvPlayLeftText.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mDataBinding.sbPlay.setProgress(Integer.parseInt(TimeUtils.millis2String(mDataBinding.videoView.getCurrentPosition(), "ss")));
            mHandler.postDelayed(this, 500);
        }
    };


    @Override
    protected int onCreate() {
        return R.layout.activity_pro_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPlayStart.setOnClickListener(this);
        mDataBinding.ivProPre.setOnClickListener(this);
        mDataBinding.ivProNext.setOnClickListener(this);
        mDataBinding.rvProVideoList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        proVideoAdapter=new ProVideoAdapter();
        for (MyVideoBean bean : listPlay) {
            bean.setSelect(false);
        }
        listPlay.get(0).setSelect(true);
        proVideoAdapter.setList(listPlay);
        mDataBinding.rvProVideoList.setAdapter(proVideoAdapter);
        proVideoAdapter.setOnItemClickListener(this);
        mCastScreenManager=CastScreenManager.getInstance();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPlayStart://暂停/播放
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.abofang);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.azant);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.ivProPre://上一个
                if (selPlayPos!=0){
                    proVideoAdapter.getItem(selPlayPos).setSelect(false);
                    proVideoAdapter.notifyItemChanged(selPlayPos);
                    selPlayPos--;
                    proVideoAdapter.getItem(selPlayPos).setSelect(true);
                    proVideoAdapter.notifyItemChanged(selPlayPos);
                    proVideo(proVideoAdapter.getItem(selPlayPos).getPath());
                    setPlayer();
                }
                isFirstEnd();
                break;
            case R.id.ivProNext://下一个
                if (selPlayPos!=listPlay.size()-1){
                    proVideoAdapter.getItem(selPlayPos).setSelect(false);
                    proVideoAdapter.notifyItemChanged(selPlayPos);
                    selPlayPos++;
                    proVideoAdapter.getItem(selPlayPos).setSelect(true);
                    proVideoAdapter.notifyItemChanged(selPlayPos);
                    proVideo(proVideoAdapter.getItem(selPlayPos).getPath());
                    setPlayer();
                }
                isFirstEnd();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        proVideoAdapter.getItem(selPlayPos).setSelect(false);
        proVideoAdapter.notifyItemChanged(selPlayPos);
        selPlayPos = position;
        proVideoAdapter.getItem(position).setSelect(true);
        proVideoAdapter.notifyItemChanged(position);
        mDataBinding.tvPlayName.setText(proVideoAdapter.getItem(position).getName());
        proVideo(proVideoAdapter.getItem(selPlayPos).getPath());
        isFirstEnd();
        setPlayer();
    }

    @Override
    protected void initData() {
        if (listPlay == null || listPlay.size() == 0) return;
        mDataBinding.tvPlayName.setText(listPlay.get(0).getName());
        //设置播放器
        setPlayer();
        isFirstEnd();
    }

    //视频投屏
    private void proVideo(String url) {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_VIDEO);
        Uri uri = Uri.parse(url);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(url);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

    private void isFirstEnd(){
        if (selPlayPos==0){
            mDataBinding.ivProPre.setImageResource(R.drawable.shangyige);
        }else{
            mDataBinding.ivProPre.setImageResource(R.drawable.ashangyg);
        }
        if (selPlayPos==listPlay.size()-1){
            mDataBinding.ivProNext.setImageResource(R.drawable.axyg);
        }else{
            mDataBinding.ivProNext.setImageResource(R.drawable.axiayigeqh);
        }
    }

    //设置播放器
    private void setPlayer() {
        mHandler = new Handler();
        videoPath = listPlay.get(selPlayPos).getPath();
        videoLength = MediaUtil.getDuration(videoPath);
        //设置seekbar
        mDataBinding.tvPlayLeftText.setText("00:00");
        mDataBinding.tvPlayRightText.setText(TimeUtil.getMmss(videoLength));
        mDataBinding.sbPlay.setMax(Integer.parseInt(TimeUtils.millis2String(videoLength, "ss")));
        mDataBinding.sbPlay.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mDataBinding.videoView.seekTo(seekBar.getProgress());
            }
        });
        //设置播放器
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                if (selPlayPos == listPlay.size() - 1) {
                    mDataBinding.tvPlayLeftText.setText("00:00");
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.abofang);
                    mDataBinding.sbPlay.setProgress(0);
                    mp.seekTo(1);
                    stopTime();
                } else {
                    proVideoAdapter.getItem(selPlayPos).setSelect(false);
                    proVideoAdapter.notifyItemChanged(selPlayPos);
                    selPlayPos++;
                    mDataBinding.tvPlayName.setText(listPlay.get(selPlayPos).getName());
                    proVideoAdapter.getItem(selPlayPos).setSelect(true);
                    proVideoAdapter.notifyItemChanged(selPlayPos);
                    setPlayer();
                }
            }
        });
        //开始播放视频
        mDataBinding.ivPlayStart.setImageResource(R.drawable.azant);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlayStart.setImageResource(R.drawable.abofang);
        mDataBinding.videoView.pause();
        stopTime();
    }

}